<?php

$value = $_GET['value'];

try {
    $mongodb = new MongoClient();
    ($value == 'article') ? $articleCollection = $mongodb->blog->articles : $articleCollection = $mongodb->blog->users;
}catch(MongoConnectionException $e) {
    die('Failed to connect to MongoDB ' . $e->getMessage());
}

$currentPage = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$articlesPerPage = 5;
$skip = ($currentPage - 1) * $articlesPerPage;

if ($value == 'article'){
    $cursor = $articleCollection->find(array() , array('title','saved_at'));
    $totalArticles = $cursor->count();
    $totalPages = (int)ceil($totalArticles / $articlesPerPage);
    $cursor->sort(array('saved_at' => - 1))->skip($skip)->limit($articlesPerPage);
}else{
    $cursor = $articleCollection->find(array() , array('username','password'));
    $totalArticles = $cursor->count();
    $totalPages = (int)ceil($totalArticles / $articlesPerPage);
    $cursor->sort(array('name' => - 1))->skip($skip)->limit($articlesPerPage);
}
?>

<html>
<head>
    <title>MongoBeer</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="bootstrap.css" />
    <link rel="stylesheet" href="style.css" />

    <script type="text/javascript" charset="utf-8">
        function confirmDelete(articleId) {
            var deleteArticle = confirm('Etes-vous sur de vouloir supprimer cet article ?');
            if(deleteArticle){
                window.location.href = 'delete.php?id='+articleId;
            }
            return;
        }

        function confirmDel(userId) {
            var delUser = confirm('Etes-vous sur de vouloir supprimer cet utilisateur ?');
            if(delUser){
                window.location.href = 'delete_user.php?id='+userId;
            }
            return;
        }
    </script>
</head>

<body>
<div id="contentarea">
    <div id="innercontentarea">
        <?php
        if ($value == 'article'){
            echo '<h1>Gestion des articles</h1>';
        }else{
            echo '<h1>Gestion des utilisateurs</h1>';
        }
        ?>
        <table class="articles" cellspacing="0" cellpadding="0">
            <thead>
            <tr>
                <?php
                if ($value == 'article'){
                    echo '<th width="45%">Titre</th><th width="30%">Créé le</th><th width="*">Action</th>';
                }else{
                    echo '<th width="45%">Nom</th><th width="30%">Login</th><th width="*">Action</th>';
                }
                ?>
            </tr>
            </thead>
            <tbody>
            <?php

            while ($cursor->hasNext()):
                $article = $cursor->getNext(); ?>
                <tr>
                    <td>
                        <?php
                        if ($value == 'article'){
                            echo $article['title'];
                        }else{
                            echo $article['username'];
                        }
                        ?>
                    </td>
                    <td>
                        <?php
                        if ($value == 'article'){
                            print date('d/m/Y H:i:s', $article['saved_at']->sec);
                        }else{
                            echo $article['password'];
                        }
                        ?>
                    </td>
                    <td class="url">
                        <?php
                        if ($value == 'article'){
                            echo '<a href="blog.php?id='.$article['_id'].'"><img src="images/loupe.gif" alt="voir" /></a>
                            <a href="edit.php?id='.$article['_id'].'"><img src="images/edit.png" alt="edit" /></a>
                            <a href="#" onclick="confirmDelete(\''.$article['_id'].'\')"><img src="images/del.png" alt="edit" /></a>';
                        }else{
                            echo '<a href="#" onclick="confirmDel(\''.$article['_id'].'\')"><img src="images/del.png" alt="edit" /></a>';
                        }
                        ?>
                    </td>
                </tr>
            <?php endwhile;?>
            </tbody>
        </table>
    </div>
    <div id="navigation">
        <div class="prev">
            <?php if($currentPage !== 1): ?>
                <a href="<?php echo $_SERVER['PHP_SELF'].' ? page = '.($currentPage - 1); ?>">Précédent </a>
            <?php endif; ?>
        </div>
        <div class="page-number">
            <?php echo $currentPage; ?>
        </div>
        <div class="next">
            <?php if($currentPage !== $totalPages): ?>
            <a href="
        <?php echo $_SERVER['PHP_SELF'].' ? page = '. ($currentPage + 1); ?>">Suivant</a><?php endif; ?>
        </div>
        <br class="clear"/>
    </div>
</div>
<div id="footer">
    <p>
        <a href="index.php">Accueil</a> -
        <a href="blogpost.php">Poster un article</a> -
        <a href="admin.php">Administration</a>
    </p>
</div>
</body>
</html>